﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace Frame.Core.Middlewares
{
    public class AuthorizeStaticFilesMiddleware
    {
        RequestDelegate _next;
        public AuthorizeStaticFilesMiddleware(RequestDelegate next)
        {
            this._next = next;
        }
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Path.StartsWithSegments("/staticFiles"))
            {
                if (context.User.Identity.IsAuthenticated)  //判断用户是否登录
                {
                    await this._next(context);
                }
                else
                {
                    await context.ForbidAsync();
                }
            }
            else
            {
                await this._next(context);
            }
        }
    }
}
